const BusinessService = require("../services/businessService")

class BusinessController {
  // 获取商家详情
  async getBusinessDetail(req, res) {
    try {
      const { business_id } = req.query
      const businessDetail = await BusinessService.getBusinessDetail(business_id)
      res.json({
        code: 0,
        data: businessDetail,
        msg: "获取成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 更新商家信息
  async updateBusiness(req, res) {
    try {
      const businessData = req.body
      await BusinessService.updateBusiness(businessData)
      res.json({
        code: 0,
        data: "success",
        msg: "更新成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 新增商家
  async addBusiness(req, res) {
    try {
      const businessData = req.body
      await BusinessService.addBusiness(businessData)
      res.json({
        code: 0,
        data: "success",
        msg: "创建成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 获取商家列表
  async getBusinessList(req, res) {
    try {
      const query = req.query
      const businessList = await BusinessService.getBusinessList(query)
      res.json({
        code: 0,
        data: businessList,
        msg: "获取成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 新增充值
  async addRecharge(req, res) {
    try {
      const rechargeData = req.body
      await BusinessService.addRecharge(rechargeData)
      res.json({
        code: 0,
        data: "success",
        msg: "充值成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 作废充值
  async invalidRecharge(req, res) {
    try {
      const { recharge_no } = req.body
      await BusinessService.invalidRecharge(recharge_no)
      res.json({
        code: 0,
        data: "success",
        msg: "作废成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }

  // 获取充值列表
  async getRechargeList(req, res) {
    try {
      const query = req.query
      const rechargeList = await BusinessService.getRechargeList(query)
      res.json({
        code: 0,
        data: rechargeList,
        msg: "获取成功",
      })
    } catch (error) {
      res.status(500).json({
        code: 500,
        msg: error.message,
      })
    }
  }
}

module.exports = new BusinessController()
